package cn.study.luckyapp.listener;

import cn.study.luckyapp.listener.event.ActivityCreateEvent;
import cn.study.luckyclient.dto.AwardUpdateCmd;
import cn.study.luckyclient.dto.data.ActivityConfigVo;
import cn.study.luckyclient.dto.data.AwardVo;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.PartialUpdate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;

@AllArgsConstructor
@Component
public class AwardInventoryToRedisApplicationListener implements ApplicationListener<ActivityCreateEvent> {
    private final RedisTemplate<String,Object> redisTemplate;
    private static final String awardInventoryKey = "lucky-draw:activity:award:";

    @Override
    public void onApplicationEvent(ActivityCreateEvent event) {
        ActivityConfigVo activityConfigVo = event.getActivityConfigVo();
        List<AwardVo> awardVo = activityConfigVo.getAwardVo();
        for(AwardVo awardVo1:awardVo){
          String key= getKey(awardVo1.getId(),awardVo1.getActivityId());
          redisTemplate.opsForValue().set(key,awardVo1.getNumber());
        }
    }
    public static String getKey(Long awardId,Long activityId) {
        return awardInventoryKey + activityId + ":" + awardId;
    }
}
